import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:package_info/package_info.dart';
import 'package:tender_flutter/constant/resource_path.dart';
import 'package:tender_flutter/constant/theme_colors.dart';
import 'package:tender_flutter/style/font_style.dart';
import 'package:tender_flutter/utils/screen_adaper.dart';
import 'package:tender_flutter/utils/toast.dart';
import 'package:tender_flutter/widgets/base_appbar.dart';
import 'package:tender_flutter/widgets/progress_indicator.dart';
import 'package:url_launcher/url_launcher.dart';

import 'api/api_person.dart';
import 'model/about_us_entity.dart';

class AboutPage extends StatefulWidget {
  @override
  _AboutPageState createState() => _AboutPageState();
}

class _AboutPageState extends State<AboutPage> {
  AboutUsEntity _aboutUsEntity;
  String _version;

  /// 获取信息
  Future _getDatas() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    _version = packageInfo.version;

    var response = await ApiPerson.getAboutUsInfo<AboutUsEntity>();
    _aboutUsEntity = response.result;
    setState(() {});
  }

  @override
  void initState() {
    super.initState();

    /// 页面渲染完成之后的回调，只执行一次
    WidgetsBinding.instance.addPostFrameCallback((mag) {
      _getDatas();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ThemeColors.colorWhite,
      appBar: _initAppBar(),
      body: _initBody(),
    );
  }

  /// 导航
  Widget _initAppBar() {
    return BaseAppBar(
      context,
      Text("关于我们", style: FonStyle.navTextStyle()),
      backgroundColor: ThemeColors.colorWhite,
    );
  }

  /// 内容体
  Widget _initBody() {
    return _aboutUsEntity == null
        ? Center(child: Progressindicator())
        : Container(
            width: double.infinity,
            child: Column(
              children: <Widget>[
                SizedBox(height: ScreenAdapter.height(60)),
                Image.asset(
                  RPath.imgPath + "icon_big_single_logo.png",
                  width: ScreenAdapter.width(160),
                  height: ScreenAdapter.width(160),
                ),
                SizedBox(height: ScreenAdapter.height(80)),
                getItem("版本信息", "V" + _version ?? "-"),
                getLine(),
                getItem("官方网站", _aboutUsEntity.web ?? "-", onTap: () async {
                  if (await canLaunch(_aboutUsEntity.web)) {
                    launch(_aboutUsEntity.web);
                  }
                }),
                getLine(),
                getItem("客服微信", _aboutUsEntity.qq ?? "-"),
                getLine(),
                // getItem("客服电话", _aboutUsEntity.phone ?? "-"),
                // getLine()
              ],
            ));
  }

  /// Item
  Widget getItem(String text, String detail, {GestureTapCallback onTap}) {
    return Container(
      padding: EdgeInsets.all(ScreenAdapter.width(30)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Text(
            text,
            style: TextStyle(
                color: ThemeColors.colorType1,
                fontSize: ScreenAdapter.sp(30),
                fontWeight: FontWeight.w500),
          ),
          GestureDetector(
              onTap: () {
                if (onTap != null) {
                  onTap();
                }
              },
              onLongPress: () {
                Clipboard.setData(new ClipboardData(text: detail));
                ToastUtils.show("复制成功");
              },
              child: Text(
                detail,
                style: TextStyle(
                    color: ThemeColors.colorType26,
                    fontSize: ScreenAdapter.sp(28),
                    fontWeight: FontWeight.w500),
              )),
        ],
      ),
    );
  }

  /// 分割线
  Widget getLine() {
    return Divider(color: ThemeColors.colorType49, height: 1);
  }
}
